package yxy.cherry.game.base.bean;

import org.redisson.api.RBucket;

import yxy.apple.util.RandomStringUtility;
import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;

public class GMInnerBean {
	private String url;
	private int port;
	private boolean ssl;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public boolean isSsl() {
		return ssl;
	}

	public void setSsl(boolean ssl) {
		this.ssl = ssl;
	}

	static private String key = "gm_inner";

	private static RBucket<GMInnerBean> touch(RedisConnection redis) throws RedisException {
		if (redis.ready()) {
			try {
				return redis.client().getBucket(key);
			} catch (Exception e) {
				throw new RedisException(e);
			}
		} else {
			throw new RedisException("无可用Redis连接!");
		}
	}

	public static void sign(RedisConnection redis, String url, int port, boolean ssl) {
		try {
			RBucket<GMInnerBean> bucket = touch(redis);

			GMInnerBean bean = bucket.get();
			if (bean == null) {
				bean = new GMInnerBean();
			}
			bean.setUrl(url);
			bean.setPort(port);
			bean.setSsl(ssl);

			bucket.set(bean);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	public static GMInnerBean get(RedisConnection redis) {
		try {
			return touch(redis).get();
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}

}
